package com.so206.config;


import com.so206.crond.process.VerfySign;
import com.so206.filter.XssFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统基础配置
 */
@Configuration
public class SystemConfig {

    private static final Logger logger = LoggerFactory.getLogger(SystemConfig.class);

    public SystemConfig() {
        VerfySign sign = new VerfySign();
        Thread thread = new Thread(sign);
        thread.setDaemon(true);
        thread.start();
        logger.info("初始化系统基本配置...");
    }

    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
        return new StringHttpMessageConverter(StandardCharsets.UTF_8);
    }

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(300);
        taskScheduler.initialize();
        return taskScheduler;
    }

    /**
     * xss过滤拦截器
     */
    @Bean
    public FilterRegistrationBean xssFilterRegistrationBean() {
        logger.info("XSS过滤器启动中....");
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new XssFilter());
        filterRegistrationBean.setOrder(1);
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addUrlPatterns("/*");
        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("excludes", "/favicon.ico,/img/*,/file/*,/css/*,/robots.txt,/fonts/*,,/images/*,/layer/*,/lib/*,/ueditor/*");
        initParameters.put("isIncludeRichText", "true");
        filterRegistrationBean.setInitParameters(initParameters);
        logger.info("XSS过滤器启动完成!");
        return filterRegistrationBean;
    }


}
